<?php 

namespace Home\Controller;
use Think\Controller;

/**
* 
*/
class SearchController extends Controller
{
	
	public function index(){
		$words = I('get.title');

		Vendor('pscws4.pscws4','','.class.php');

		$pscws = new \PSCWS4('utf8');

		$pscws->set_dict('./Public/dict.utf8.xdb');

		$pscws->send_text($words);
		/*while ($some = $pscws->get_result())
		{
		   foreach ($some as $word)
		   {
		      var_dump($word);
		   }
		}*/

		$tops = $pscws->get_tops(3, 'n,v');

		$sql = "select * from question where ";


		foreach ($tops as $v) {
			$sql .= "title like '%".$v['word']."%' or ";
		}
		$sql = rtrim($sql,'or ');

		$main = M('question')->query($sql);
		
		foreach ($main as $key => $value) {
			$i=0;
			foreach ($tops as $v) {
				$restr[$i] = '<em>'.$v['word'].'</em>';
				$curstr[$i] = $v['word'];
				$i++;
			}
			
			$main[$key]['title'] = str_replace($curstr,$restr,$main[$key]['title']);
			//echo $main[$key]['title'];
		}
		//var_dump($str);exit;

		$this-> assign('main',$main);
		$this->display();
	}

}

 ?>